﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mail;

namespace SMTPDemo
{

    class Program
    { //some const string to show the mail infomation 
        //accout info would be replaced.  

        private const string mailFrom = "daniel.0918.mit@gmail.com";
        private const string mailTo = "shen2002fei@126.com";
        private const string mailSubject = "Just a test Mail";

        //some const string to create the smtp server and user 

        private const string sendServer = "smtp.gmail.com";
        private const string sendUsr = "daniel.0918.mit@gmail.com";
        private const string sendPwd = "op87216151";

        public static void Main(string[] args)
        {
            //create a mail to send 

            MailMessage myMail = new MailMessage();

            myMail.From = mailFrom;//email from 

            myMail.To = mailTo;//email to: reciever's email address 
            myMail.Subject = mailSubject;//the email's subject 
            myMail.Body = "You are really success.";//the email's body 
            myMail.BodyEncoding = Encoding.UTF8;//the coding, in windows, usually is utf-8 
            myMail.BodyFormat = MailFormat.Text;//the body format html or text; 
            myMail.Attachments.Add(new MailAttachment(@"d:\JobStatus_12282011.xls", MailEncoding.Base64));
            //myMail.Cc = ""; // 
            //myMail.Bcc = ""; 
            //myMail.Priority = MailPriority.High | MailPriority.Low | MailPriority.Normal;   

            //set the mail to be certify needed 
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);

            //myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);

            //set the user to be certified 
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusername", sendUsr);

            //the password of the account 
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtppassword", sendPwd);

            SmtpMail.SmtpServer = sendServer;

            try
            {
                SmtpMail.Send(myMail);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception throw out:{0}", e.Message);
            }

            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();
        }
    }
}
